<?php
class Managesoftware_Data_Editaction {
	
	private $database;
	private $secure;
	
	public function __construct(){
		$this->secure = new Core_Data_Secure();
		$this->database = new Core_Driver_MysqlImproved();
	}
	
	public function updateValues(array $postVars){
		$postVars = $this->secure->parseData($postVars);
		$oldSoftwareName = $postVars['oldSoftwareName'];
		$name = $postVars['name'];
		$metaTitle = $postVars['meta_title'];
		$accessLevel = $postVars['privileges'];
		$metaKeywords = $postVars['meta_keywords'];
		$metaDescription = $postVars['meta_description'];
		$title = $postVars['title'];
		$description = $postVars['description'];
		$content = $postVars['content'];
		
		$this->updateSoftware($oldSoftwareName, $name, $accessLevel);
		$this->updateSoftwareMetaValues($name, $metaTitle, $metaKeywords, $metaDescription);
		$this->updateSoftwareContent($name, $title, $description, $content);
		
		echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?managesoftware">';
	}
	
	private function updateSoftwareContent($name, $title, $description, $content){
		$this->database->connect();
		$oldSoftwareName = $this->database->escape($oldSoftwareName);
		$name = $this->database->escape($name);
		$title = $this->database->escape($title);
		$description = $this->database->escape($description);
		$content = $this->database->escape($content);
		
		$this->database->prepare(
			"
				update software_content_values set software_name = '$name', software_title = '$title', software_description = '$description', software_content = '$content' where software_name = '$name';
				"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
	
	private function updateSoftwareMetaValues($name, $metaTitle, $metaKeywords, $metaDescription){
		$this->database->connect();
		$name = $this->database->escape($name);
		$metaTitle = $this->database->escape($metaTitle);
		$metaDescription = $this->database->escape($metaDescription);
		$metaKeywords = $this->database->escape($metaKeywords);
		$this->database->prepare(
			"
				update software_meta_values set software_name = '$name', software_meta_title = '$metaTitle', software_meta_keywords = '$metaKeywords', software_meta_description = '$metaDescription' where software_name = '$name';
				"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
	
	private function updateSoftware($oldSoftwareName, $name, $accessLevel){
		$this->database->connect();
		$oldSoftwareName = $this->database->escape($oldSoftwareName);
		$name = $this->database->escape($name);
		$accessLevel = $this->database->escape($accessLevel);
		$this->database->prepare(
			"
				update software set software_name = '$name', software_access_level = '$accessLevel' where software_name = '$oldSoftwareName';
				"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
}